package com.xique.door.controller;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.request.deviceUser.DeviceAddUserRequest;
import com.xique.door.bean.request.deviceUser.DeviceBatchUserListRequest;
import com.xique.door.bean.request.deviceUser.DeviceDeleteUserRequest;
import com.xique.door.bean.request.deviceUser.DeviceUserSelectListRequest;
import com.xique.door.bean.vo.user.UserListVo;
import com.xique.door.biz.service.IDeviceUserBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shd
 * @create 2022/1/28 14:20
 */
@RestController
@RequestMapping("/deviceUser")
public class DeviceUserController extends BaseController {

    @Autowired
    private IDeviceUserBizService deviceUserBizService;

    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @GetMapping("/assigned/userList")
    public TableDataInfo list(DeviceUserSelectListRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        List<UserListVo> userListVos = deviceUserBizService.selectAssignedDeviceUserList(request);
        return getDataTable(userListVos);
    }

    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @GetMapping("/unAssigned/userList")
    public AjaxResult unAssignedUserList(DeviceUserSelectListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        startPage();
        return deviceUserBizService.selectUnAssignedDeviceUserList(request);
    }

    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @PostMapping("/command/addUser")
    public AjaxResult addDeviceUser(@RequestBody DeviceAddUserRequest request) {
        if (request.getDeviceId() == null || (request.getUserId() == null && StringUtils.isEmpty(request.getUserIds()))){
            return AjaxResult.error("请检查是否勾选用户和设备");
        }
        request.setParkId(SecurityUtils.getParkId());
        return deviceUserBizService.addDeviceUser(request);
    }


    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @PostMapping("/command/removeUser")
    public AjaxResult removeDeviceUser(@RequestBody DeviceDeleteUserRequest request) {
        if (request.getDeviceId() == null || (request.getUserId() == null && StringUtils.isEmpty(request.getUserIds()))){
            return AjaxResult.error("请检查是否勾选用户和设备");
        }
        request.setParkId(SecurityUtils.getParkId());
        return deviceUserBizService.removeDeviceUser(request);
    }

    /**
     * 全量下发设备中已有的用户
     */
    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @PostMapping("/command/addAllUser")
    public AjaxResult addAllDeviceUser(@RequestBody DeviceDeleteUserRequest request) {
        if (request.getDeviceId() == null ){
            return AjaxResult.error("必填项不能为空");
        }
        request.setParkId(SecurityUtils.getParkId());
        return deviceUserBizService.addAllDeviceUser(request);
    }

    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @GetMapping("/allDevices/userList")
    public TableDataInfo allDevicesUserList(DeviceUserSelectListRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        List<UserListVo> userListVos = deviceUserBizService.selectAllDevicesUserList(request);
        return getDataTable(userListVos);
    }

    @PreAuthorize(hasPermi = "door:deviceUser:list")
    @PostMapping("/command/batchUser")
    public AjaxResult addBatchUser(@RequestBody DeviceBatchUserListRequest request) {
        return deviceUserBizService.addBatchDeviceAndUser(request);
    }
}
